<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\SettlementGame;
use app\common\model\CpChannel;
use app\common\model\Channel;
use app\services\Settlement;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Settle extends AdminBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 结算账单
     * @return list
     * 按照月份统计结算
     */
     public function index(){
         $params = $this->request->param();

         $settle = new SettlementGame();
         $where = [];
         if(!empty($params['gid'])){
             $where[] = ['gid', '=', $params['gid']];
         }else{
             $where[] = ['gid', '>', '0'];
         }

         if(!empty($params['cid'])){
             $where[] = ['cid', '=', $params['cid']];
         }

         if (!empty($params['starttime'])) {
             $where[]= ['date_month','=',$params['starttime']];
         }

         $list = $settle->field('id,cp_id,cid,gid,date_month,sum(amount) as amount,
                                sum(g_fencheng) as g_fencheng,sum(c_fencheng) as c_fencheng,sum(g_fee) as g_fee,status,create_time')
             ->where($where)
             ->group("date_month")
             ->order("id desc")
             ->paginate(100);
         $game_list = get_game_list();
         $this->assign("game_list",$game_list);
         $this->assign("counts",count($list));

         // 转换为数组以便返回
         return $this->fetch('index', ['list' => $list]);
     }

     public function detail()
     {
         $params = $this->request->param();
         $params['date_month'] = date("Y-m",$params['date_month']);

         $settle = new SettlementGame();
         $where = [];
         if(!empty($params['gid'])){
             $where[] = ['gid', '=', $params['gid']];
         }else{
             $where[] = ['gid', '>', '0'];
         }

         if (!empty($params['date_month'])) {
             $where[]= ['date_month','=',$params['date_month']];
         }
         if (!empty($params['cp_id'])) {
             if($params['type'] == 2){
                 $where[]= ['cid','=',$params['cp_id']];
             }else{
                 $where[]= ['cp_id','=',$params['cp_id']];
             }
         }

         $list = $settle
             ->where($where)
             ->order("id desc")
             ->select();
         $game_list = get_game_list();
         $this->assign("game_list",$game_list);
         $this->assign("counts",count($list));

         // 转换为数组以便返回
         return $this->fetch('detail', ['list' => $list]);

     }

     public function yulan(){
         $params = $this->request->param();
         $params['date_month'] = date("Y-m",$params['date_month']);

         $settle = new SettlementGame();
         $where = [];
         if(!empty($params['gid'])){
             $where[] = ['gid', '=', $params['gid']];
         }else{
             $where[] = ['gid', '>', '0'];
         }

         if (!empty($params['date_month'])) {
             $where[]= ['date_month','=',$params['date_month']];
         }
         if (!empty($params['cp_id'])) {
             if($params['type'] == 2){
                 $where[]= ['cid','=',$params['cp_id']];
             }else{
                 $where[]= ['cp_id','=',$params['cp_id']];
             }
         }

         $list = $settle
             ->where($where)
             ->order("id desc")
             ->select();

         // 填充数据

         $sumData= [
             'totalAmount'=>0,
             'totalFee'=>0,
             'totalIncome'=>0,
             'totalCIncome'=>0,
             ];

         if(!empty($list)){
             foreach ($list as $item) {
                 $sumData['totalAmount'] += $item['amount'];
                 $sumData['totalFee'] += $item['g_fee'];
                 $sumData['totalIncome'] += $item['g_fencheng'];
                 $sumData['totalCIncome'] += $item['c_fencheng'];
             }
         }
         if($params['type'] == 2){
             // 渠道信息
             $cpData = Channel::find($params['cp_id']);
         }else{
             // cp信息
             $cpData = CpChannel::find($params['cp_id']);
         }

         $this->assign('cpData', $cpData);

         $game_list = get_game_list();
         $this->assign('sumData',$sumData);
         $this->assign("game_list",$game_list);
         $this->assign("counts",count($list));


         // 转换为数组以便返回
         return $this->fetch('yulan', ['list' => $list]);
     }

     public function settle()
     {
         $params = $this->request->param();
         if(empty($params['cp_id'])){
             $this->error("结算信息错误。");
         }
         if(empty($params['date_month'])){
             $this->error("结算周期错误。");
         }

         $where = [];

         $where[]= ['date_month','=',$params['date_month']];
         $where[]= ['cp_id','=',$params['cp_id']];

         // 修改结算数据
         $settle = new SettlementGame();
         if($settle->where($where)->update(['status'=>1])){
             $this->success("结算成功！");
         }
         $this->error("结算失败。");
     }

    /**
     * 高德结算账单
     * @return list
     * 按照月份统计结算
     */
    public function gaode(){
        $params = $this->request->param();

        $settle = new SettlementGame();
        $where = [];
        if(!empty($params['gid'])){
            $where[] = ['gid', '=', $params['gid']];
        }else{
            $where[] = ['gid', '>', '0'];
        }
        if(!empty($params['cid'])){
            $where[] = ['cid', '=', $params['cid']];
        }else{
            $where[] = ['cid', '=', '5'];

        }

        if (!empty($params['starttime'])) {
            $where[]= ['date_month','=',$params['starttime']];
        }

        $list = $settle->field('id,cp_id,cid,gid,date_month,sum(amount) as amount,
                                sum(g_fencheng) as g_fencheng,sum(c_fencheng) as c_fencheng,sum(g_fee) as g_fee,status,create_time,c_status')
            ->where($where)
            ->group("cid")
            ->order("id desc")
            ->paginate(100);

        $game_list = get_game_list();
        $this->assign("game_list",$game_list);
        $this->assign("counts",count($list));

        // 转换为数组以便返回
        return $this->fetch('gaode', ['list' => $list]);
    }

}